package com.naizhuai.util;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.naizhuai.R;
import com.naizhuai.ui.adapter.DialogCheckedAdapterHolder;
import com.naizhuai.ui.adapter.ManageDividerItemDecoration;
import com.naizhuai.ui.entity.management.ChoseUserAdapterBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 点击选择personUserList
 * Created by code_han on 2018/3/5.
 */

public class CheckEntityByDialogUtils<T> {

    private Context mContext;

    private List<T> sourceEntityList;

    private AddPersonListener<T> addListener;

    private int limitNumber;

    private String TAG;

    public CheckEntityByDialogUtils(Context mContext, List<T> sourceEntityList, int limitNumber, String TAG, AddPersonListener<T> addListener) {
        this.mContext = mContext;
        this.addListener = addListener;
        this.sourceEntityList=sourceEntityList;
        this.limitNumber=limitNumber;
        this.TAG=TAG;
    }

    public void initView() {
        View dialogView = initDialogView();

        DialogUtils.showCustomizeDialog(mContext, null, dialogView, new DialogUtils.DialogListener() {
            @Override
            public boolean onClick(View view) {
                RecyclerView recyclerView= (RecyclerView) dialogView.getTag();
                DialogCheckedAdapterHolder checkedAdapterHolder= (DialogCheckedAdapterHolder) recyclerView.getAdapter();
                List<T> users=checkedAdapterHolder.getCheckedEntity();

                if (users.size()==0) {
                    ToastCompat.showToast(mContext, mContext.getText(R.string.chose_no_user).toString(), Toast.LENGTH_SHORT);
                    return false;
                }else if(limitNumber!=-1&&users.size()>limitNumber){
                    String strToast=String.format("%s只允许选取%s个", TAG, String.valueOf(limitNumber));
                    ToastCompat.showToast(mContext, strToast, Toast.LENGTH_SHORT);
                }
                addListener.onClick(users);
                return true;
            }
        });
    }

    private View initDialogView() {
        List<ChoseUserAdapterBean<T>> userChosenList=new ArrayList<>();
        for (T entity:sourceEntityList) {
            userChosenList.add(new ChoseUserAdapterBean<>(entity,ConstantsState.UNCHECKED));
        }
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_odd_person_search_list, null);
        DialogCheckedAdapterHolder checkedAdapterHolder=new DialogCheckedAdapterHolder(mContext,userChosenList);
        RecyclerView chosenList = dialogView.findViewById(R.id.chose_member_list);
        chosenList.setHasFixedSize(true);
        chosenList.setLayoutManager(new LinearLayoutManager(mContext));
        chosenList.setItemAnimator(new DefaultItemAnimator());
        ManageDividerItemDecoration dividerDecoration=new ManageDividerItemDecoration(mContext,ManageDividerItemDecoration.VERTICAL_LIST,5,true);
        dividerDecoration.setBaseFloat(0.3f);
//        dividerDecoration.setDivideIndex(new int[]{publicTeams.size()-1,publicTeams.size()+oddPeople.size()-1});
        chosenList.addItemDecoration(dividerDecoration);
        chosenList.setAdapter(checkedAdapterHolder);
        dialogView.setTag(chosenList);
        return dialogView;
    }
    
    public interface AddPersonListener<T> {
        void onClick(List<T> user);

    }
    
    

}
